<?php
namespace Admin\Controller;
use Think\Controller;

/**
 * Class AdminController
 * @package Admin\Controller
 * 个人信息
 */
class AdminController extends Controller {

    public function index(){
        $this->display();
    }

    public function editinfo(){
        $user = $this->get_user();
        $this->assign('user', $user);
        $this->display('edit_info');
    }

    public function set_user_info(){
        if(IS_POST){
            $user_infor = get_admin_session_array();
            $uid = $user_infor['user_id'];
            $user_model = D('Admin');
            $one = $user_model->set_user($_POST);
            if($one){
                $this->success('保存成功');
            }else{
                $this->error('保存失败');
            }
        }
    }

    public function change_pwd(){
        $user = $this->get_user();
        $this->assign('user', $user);
        $this->display('change_pwd');
    }

    public function set_pwd(){
        $pwds = filterInput($_POST);
        $old_pwd = $pwds['old_password'];
        $new_pwd = $pwds['new_password'];
        $new_confirm = $pwds['new_pwdconfirm'];
        $user = $this->get_user();
        $result = array();
        if($user['password'] != md5(md5($old_pwd))){
            $result['status'] = 1;
            $this->error('原始密码错误');
        }else if($new_confirm != $new_pwd){
            $result['status'] = 2;
            $this->error('两次输入密码不相同');
        }else if(($user['password'] == md5(md5($old_pwd))) && ($new_confirm == $new_pwd)){
            $user['password'] = md5(md5($new_confirm));
            $this->set_user($user);
            $result['status'] = 0;
            $this->success('保存成功');
        }

    }

    private function get_user(){
        $uid = get_module_session(get_kv_val('session_pix').'admin', 'user_id');
        $user_model = D('Admin');
        $user = $user_model->get_user($uid);
        return $user;
    }

    private function set_user($user){
        $user_model = D('Admin');
        $one = $user_model->set_user($user);
        return $one;
    }

}